package com.example.computer_exam_training_sys.mapper;

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserProgressMapper {

    // 获取某课程已完成的知识点数量
    @Select("SELECT COUNT(*) FROM user_point_progress WHERE user_id = #{userId} AND course_id = #{courseId} AND completed = 1")
    int countCompletedPoints(@Param("userId") Integer userId, @Param("courseId") Integer courseId);

    // 判断某个知识点是否已完成（用于标记前先判断）
    @Select("SELECT COUNT(*) FROM user_point_progress WHERE user_id = #{userId} AND point_id = #{pointId} AND completed = 1")
    int exists(@Param("userId") Integer userId, @Param("pointId") Integer pointId);

    // 更新为已完成状态
    @Update("UPDATE user_point_progress SET completed = 1, completed_time = NOW() WHERE user_id = #{userId} AND point_id = #{pointId}")
    void updateCompleted(@Param("userId") Integer userId, @Param("pointId") Integer pointId);

    // 插入新完成记录
    @Insert("INSERT INTO user_point_progress (user_id, point_id, course_id, completed) VALUES (#{userId}, #{pointId}, #{courseId}, 1)")
    void insertCompleted(@Param("userId") Integer userId, @Param("pointId") Integer pointId, @Param("courseId") Integer courseId);

    // 获取已完成的 pointId 列表（用于前端展示标记状态）
    @Select("SELECT point_id FROM user_point_progress WHERE user_id = #{userId} AND course_id = #{courseId} AND completed = 1")
    List<Integer> getCompletedPointIds(@Param("userId") Integer userId, @Param("courseId") Integer courseId);
}
